function Options = GridStrainOptions
%GridStrainOptions - GridStrain general options management
%
%       GridStrainOptions
%       Options = GridStrainOptions
%
% If no output is considered, a modal box allows the management of main
% options related to GridStrain and save the file GSOptions.mat.
% If an output is considered, no modal box is shown. In this case, if the 
% file GSOptions.mat exists, the options related to this file are used. If 
% the file GSOptions.mat does not exist, the default choices are used.
% Options:
%               Option               Possible values       Default
%       Options.VisualizePoints         true/false          true
%       Options.VisualizePointIDs       true/false          true
%       Options.VisualizeGridHSA        true/false          true
%       Options.VisualizeContourHSA     true/false          true
%       Options.SmoothContour           true/false          false
%       Options.HSFourQuadrants         true/false          false
%       Options.Function             'exp'/'gaussian'       'exp'
%
% where 
% - Options.VisualizePoints is referred to the visualization of experimental 
%   points in strain plotting (i.e. this option does not apply in plotting 
%   for data loading and possible selection and removal of some stations);
% - Options.VisualizePointIDs is referred to the visualization of point IDs
%   (numeric or station name, depending on input file) in both dataload 
%   and strain plotting (in the case of strain plotting, this option acts
%   under the condition that Options.VisualizePoints is true);
% - Options.VisualizeGridHSA is referred to the grid visualization in case  
%   of plotting in high significance areas (or high and mean significance 
%   areas);
% - Options.VisualizeContourHSA is referred to the visualization of contour 
%   plots of strain in high significance areas (HSA) or in high and mean 
%   significance areas (HMSA);
% - Options.SmoothContour is referred to the contour plots. If it is true,
%   the contout is smoothed on a 5-by-5 rectangle and no edges are plotted.
%   If it is false (default option), no smoothing is carried out and the 
%   edges are plotted. 
% - Options.HSFourQuadrants is referred to the spatial distributions of
%   points to have high geometric significance (if true, four 90° quadrants,
%   if false three 120° sectors).
% - Options.Function is referred to the choice of the function, i.e. 'exp' 
%   for exp(-d/d0) and 'gaussian' for exp(-(d/do).^2). 
% 
% If the user interactively selects two incompatible choices (e.g. true
% and false for VisualizePoints), the corresponding default value is used.
% In the no-choice case, all default values are used.   

% G. Teza, 2022

nOpt = 15;

if nargout == 0
    
    Options = [];
    OptVec = false(nOpt-1,1);   % choice vector
    defChoice = false;  % if true, default options are used
    
    listMan = {...
        'Visualize experimental points YES',...
        'Visualize experimental points NO',...
        'Visualize point IDs YES',...
        'Visualize point IDs NO',...
        'Visualize grid High Significance areas YES',...
        'Visualize grid High Significance areas NO',...
        'Visualize contouring High Significance areas YES',...
        'Visualize contouring High Significance areas NO',...
        'Smoothing contour plots YES',...
        'Smoothing contour plots NO',...
        'High significance four 90° quadrants',...
        'High significance three 120° quadrants',...
        'Use exponential function',...
        'Use gaussian function',...
        'Default options'}; 
    
    [indx,tf] = listdlg(...
        'PromptString',{'GridStrain options'},...
        'ListString',listMan,...
        'InitialValue',1:2:(nOpt-2),...
        'ListSize',[300 200]);

    if tf
        if indx(end) == nOpt
            defChoice = true;
        elseif numel(indx) > 1
            for k = 1:(numel(indx)-1)   % to solve the problem of a bad choice
                if (rem(indx(k),2) > 0) && (indx(k+1) == indx(k)+1)
                    indx(k+1) = indx(k);
                end
            end
            unique(indx);
        end
    else
        defChoice = true;
    end
    if defChoice
        indx = 1:2:(nOpt-1);
    end
    
    for h = 1:(nOpt-1)
        if ismember(h,indx)
            OptVec(h) = true;
        end
    end

    if OptVec(1)
        Options.VisualizePoints = true;
    else
        Options.VisualizePoints = false;
    end
    if OptVec(3)
        Options.VisualizePointIDs = true;
    else
        Options.VisualizePointIDs = false;
    end
    if OptVec(5)
        Options.VisualizeGridHSA = true;
    else
        Options.VisualizeGridHSA = false;
    end
    if OptVec(7)
        Options.VisualizeContourHSA = true;
    else
        Options.VisualizeContourHSA = false;
    end
    if OptVec(9)
        Options.SmoothContour = true;
    else
        Options.SmoothContour = false;
    end
    if OptVec(11)
        Options.HSFourQuadrants = true;
    else
        Options.HSFourQuadrants = false;
    end
    if OptVec(nOpt-2)
        Options.Function = 'exp';
    else
        Options.Function = 'gaussian';
    end
    save('GSOptions','Options');

else
    
    Options = [];
    if exist('GSOptions.mat','file')
        GSO = load('GSOptions.mat');
        Options = GSO.Options;
    else    % Default options
        Options.VisualizePoints = true;
        Options.VisualizePointIDs = true;
        Options.VisualizeGridHSA = true;
        Options.VisualizeContourHSA = true;
        Options.SmoothContour = false;
        Options.HSFourQuadrants = false;
        Options.Function = 'exp';
        save('GSOptions','Options');
    end

end